package com.chenlei.toppopup.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.chenlei.toppopup.viewholder.ViewHolder;

import java.util.List;

/**
 * Created by chenlei on 2017/12/13.
 */

public abstract class CLBasePopupAdapter<T> extends RecyclerView.Adapter<ViewHolder>
{
  protected Context mContext;
  private List<T> mDatas;
  private int mLayoutId;
  public OnItemClickListener mListener;

  public CLBasePopupAdapter(Context context, List<T> datas, int layoutId)
  {
    mDatas = datas;
    mContext = context;
    mLayoutId = layoutId;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
  {

    return ViewHolder.get(mContext, parent, mLayoutId);
  }

  @Override
  public void onBindViewHolder(ViewHolder holder, int position)
  {
    convert(holder, mDatas.get(position), position);
  }

  @Override
  public int getItemCount()
  {
    return mDatas.size();
  }

  public abstract void convert(ViewHolder holder, T t, int position);


  public void updateAdapterClearBeforeDatas(List<T> list)
  {
    mDatas.clear();
    mDatas.addAll(list);
    notifyDataSetChanged();
  }

  /**
   * 延时启动
   *
   * @param view View
   */
  protected final void postDelayed(View view)
  {
    view.postDelayed(new Runnable()
    {
      @Override
      public void run()
      {
        postDelayedStart();
      }
    }, 200);
  }

  protected void postDelayedStart()
  {

  }

  public interface OnItemClickListener
  {
    void onItemClick(int pos);
    void onItemInputText(String text);
  }

  public void setOnItemClickListener(OnItemClickListener listener)
  {
    mListener = listener;
  }
}
